package com.canyou.utils;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Date;

/**
 * 路径帮助类
 *
 * @author fy
 * @date 20/02/18 19:53
 */
public final class PathUtils {

    private PathUtils() {
    }

    /**
     * 判断路径是否是绝对路径
     *
     * @param path
     * @return
     */
    public static boolean isAbsolutePath(String path) {
        return path.startsWith("/") || path.indexOf(':') == 1;
    }

    /**
     * 拼接两个路径
     *
     * @param path
     * @param more
     * @return
     */
    public static String concatPath(String path, String... more) {
        return Paths.get(path, more).toString();
    }

    /**
     * 获取文件存储的基础物理路径
     *
     * @param uploadPath
     * @return
     * @throws FileNotFoundException
     */
    public static String getBaseAbsolutePath(String uploadPath) throws FileNotFoundException {
        if (PathUtils.isAbsolutePath(uploadPath)) {
            return uploadPath;
        }
        return PathUtils.getWebRootPath();
    }

    /**
     * 获取上传文件存储的dir路径
     *
     * @param uploadPath
     * @param now
     * @return
     * @throws FileNotFoundException
     */
    public static String getDirAbsolutePath(String uploadPath, Date now) throws FileNotFoundException {
        String relativePath = getRelativeDirPath(uploadPath, now);
        if (PathUtils.isAbsolutePath(uploadPath)) {
            return concatPath(uploadPath, relativePath);
        }
        return concatPath(PathUtils.getWebRootPath(), relativePath);
    }

    /**
     * 获取文件的存储相对路径
     *
     * @param basePath
     * @param now
     * @return
     */
    public static String getRelativeDirPath(String basePath, Date now) {
        final String separator = File.separator;
        String lastPath = separator +
                DateFormatUtils.format(now, "yyyy") +
                separator +
                DateFormatUtils.format(now, "MM");
        if (!isAbsolutePath(basePath)) {
            return separator + basePath + lastPath;
        }
        return lastPath;
    }

    /**
     * 获取文件的访问相对路径
     *
     * @param basePath
     * @param now
     * @return
     */
    public static String getRelativeUrlPath(String basePath, Date now) {
        String lastPath = "/" +
                DateFormatUtils.format(now, "yyyy") + "/" +
                DateFormatUtils.format(now, "MM");
        if (!isAbsolutePath(basePath)) {
            return "/" + basePath + lastPath;
        }
        return lastPath;
    }

    /**
     * 获取项目web root物理路径
     *
     * @return
     */
    public static String getWebRootPath() throws FileNotFoundException {
        return FileUtil.getWebRoot().getAbsolutePath();
        //return new File(ResourceUtils.getURL("classpath:").getPath(), "/static/").getAbsolutePath();
        //return request.getServletContext().getRealPath("/");
        //return ClassUtils.getDefaultClassLoader().getResource("static").getPath();
    }

    /**
     * 判断目录是否存在, 不存在则创建
     *
     * @param absolutePath
     */
    public static void checkPathAndCreate(final String absolutePath) {
        ////FileUtil.exist(absolutePath);
        ////FileUtil.mkdir(absolutePath);
        // 同时上传多个文件可能存在并发创建根目录的问题
        synchronized (PathUtils.class) {
            File dir = new File(absolutePath);
            if (!dir.exists()) {
                if (!dir.mkdirs()) {
                    throw new RuntimeException("文件夹 " + absolutePath + " 不存在, 并且无法创建该文件夹.");
                }
            }
        }
    }

    /**
     * 获取 Resource 文件夹下资源文件
     * resourcePath:
     * template/物业信息查询.xlsx
     * static/icon.png
     *
     * @param resourcePath 资源相对路径
     * @return
     * @throws IOException
     */
    public static File getClassPathResourceFile(String resourcePath) throws IOException {
        /// String baseName = FilenameUtils.getBaseName(resourcePath);
        String name = FilenameUtils.getName(resourcePath);
        String ext = FilenameUtils.getExtension(resourcePath);
        ClassPathResource classPathResource = new ClassPathResource(resourcePath);
        InputStream inputStream = classPathResource.getInputStream();
        File tempFile = File.createTempFile(name, ext);
        FileUtils.copyToFile(inputStream, tempFile);
        return tempFile;
    }

    /**
     * 获取 Resource 文件夹下资源文件录
     * resourcePath:
     * template/物业信息查询.xlsx
     * static/icon.png
     *
     * @param resourcePath 资源相对路径
     * @return
     * @throws IOException
     */
    public static String getClassPathResourcePath(String resourcePath) throws IOException {
        return getClassPathResourceFile(resourcePath).getAbsolutePath();
    }

    /**
     * 获取一个临时目录
     *
     * @return
     */
    public static String getTmpPath() {
        return PathUtils.concatPath(FileUtil.getTmpDir().getAbsolutePath(), StringUtils.uuid());
    }

}
